package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/courses")
    public Result selectList(CourseDto courseDto){
        log.info("查询课程{}",courseDto);
        PageResult<Course> list =courseService.selectList(courseDto);
        return Result.success(list);
    }

    @DeleteMapping("/courses/{id}")
    public Result deleteById(Integer id){
        log.info("删除课程{}",id);
        courseService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/courses")
    public  Result add(@RequestBody Course course){
        log.info("添加课程");
        courseService.add(course);
        return Result.success();
    }

    @GetMapping("/courses/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询课程{}",id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @PutMapping("/courses")
    public Result update(@RequestBody Course course){
        log.info("更新课程");
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/courses/list")
    public Result allList(){
        log.info("查询所有课程");
        List<Course> list = courseService.allList();
        return Result.success(list);
    }

    @GetMapping("/courses/subject/{subject}")
    private Result subjectSelect(@PathVariable Integer subject){
        log.info("查询课程{}", subject);
        List<Course> list = courseService.subjectSelect(subject);
        return Result.success(list);
    }
}
